package com.ych.springmvc.controller;

import com.ych.springmvc.pojo.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * @author yangchenghao
 */
@Controller
public class TestAjax {
    @PostMapping("/test/ajax")
    public void testAJax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println("id:" + id);
        System.out.println("requestBody:" + requestBody);
        response.getWriter().write("hello,axios");
    }

    @PostMapping("/requestbody")
    public void testRequestBody(Integer id, @RequestBody User user, HttpServletResponse response) throws IOException {
        System.out.println("id:" + id);
        System.out.println("requestBody:" + user);
        response.getWriter().write("hello,axios");
    }

    @GetMapping("/success")
    @ResponseBody
    public String testResponse() {
        return "success";
    }

    @GetMapping("/test/download")
    public ResponseEntity<byte[]> testDownload(HttpSession session) throws Exception {
        ServletContext servletContext = session.getServletContext();
        String img = servletContext.getRealPath("img");
        String fileName = img + File.separator + "蜘蛛侠.jpeg";
//        System.out.println(fileName);
        FileInputStream is = new FileInputStream(new File(fileName));
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        //创建HttpHeaders对象设置响应头信息
        MultiValueMap<String, String> headers = new HttpHeaders();
        //设置要下载方式以及下载文件的名字
        headers.add("Content-Disposition", "attachment;filename=1.jpg");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
        is.close();
        return new ResponseEntity<>(bytes, headers, statusCode);
    }

    @PostMapping("/test/upload")
    public String testUpload(HttpSession session, MultipartFile photo) throws IOException {
        String originalFilename = photo.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(index);
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("img");
        File file = new File(photoPath);
        if (!file.exists()) {
            file.mkdir();
        }
        String filePath = photoPath + File.separator + fileName;
        photo.transferTo(new File(filePath));
        return "success";
    }
}
